package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/*
打开File -> Settings -> Editor -> File And Code Template -> Includes -> File Header 声明文件通用文档注释信息
/**
 * PROJECT_NAME: ${PROJECT_NAME}
 * NAME: ${NAME}
 * USER: ${USER}
 * DATE: ${DATE}
 * DESCRIPTION :
 */

/**
 * PROJECT_NAME: sky-take-out
 * NAME: EmployeeController
 * USER: SHINIAN
 * DATE: 2022/12/12
 * DESCRIPTION : B端-员工信息表现层接口
 */
@Slf4j
@Api(tags = "B端-员工信息表现层接口") //@Api:用户描述类的相关信息
@RestController
@RequestMapping("/admin/employee") //声明访问路径为/admin/employee自动映射到此方法
public class EmployeeController {

    //注入员工服务层实现类Bean
    @Autowired
    private EmployeeService employeeService;
    //注入Jwt令牌信息参数封装Bean(包含秘钥)
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * B端-员工登录功能
     *
     * @param employeeLoginDTO 员工登录信息的DTO(用户名+密码)
     * @return 全局通用返回信息(员工登录响应信息VO : 包含Jwt令牌给前端用户下次访问携带)
     */
    @ApiOperation("B端-员工登录方法")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("B端员工登录参数 : {}", employeeLoginDTO);
        //基于服务层完成员工信息的查询
        Employee employee = employeeService.login(employeeLoginDTO);
        //根据查询出的用户信息生成Jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId()); //将员工Id保存到令牌部分(2)中
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(), //秘钥
                jwtProperties.getAdminTtl(), //过期时间
                claims); //令牌数据
        //封装员工登录响应信息VO(包含员工的基本信息+令牌数据)并返回
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId()).userName(employee.getUsername())
                .name(employee.getName()).token(token).build();
        return Result.success(employeeLoginVO);
    }

    /**
     * 退出登录功能(返回响应成功的Result即可,前端会自动将TOKEN令牌信息删除,下次请求不会携带)
     *
     * @return 全局通用返回信息
     */
    @ApiOperation("B端-员工退出登录方法")
    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }

    @ApiOperation("B端-员工分页查询")
    @GetMapping("/page")
    public Result getPage(EmployeePageQueryDTO dto){
        log.info("【员工分页查询】");
        return employeeService.getPage(dto);
    }

    @ApiOperation("B端-新增员工")
    @PostMapping()
    public Result insertEmployee(@RequestBody EmployeeDTO dto){
        log.info("【新增员工】");
        employeeService.insertEmployee(dto);
        return Result.success();
    }

    @ApiOperation("B端-根据id查询员工信息")
    @GetMapping("/{id}")
    public Result<Employee> selectEmployeeById(@PathVariable Long id){
        log.info("【根据id查询员工信息】");
        Employee employee = employeeService.selectEmployeeById(id);
        return Result.success(employee);
    }

    @ApiOperation("B端-修改员工信息")
    @PutMapping
    public Result updateEmployee(@RequestBody EmployeeDTO employeeDTO){
        log.info("【修改员工信息】");
        employeeService.updateEmployee(null,employeeDTO);
        return Result.success();
    }

    @ApiOperation("B端-修改员工状态")
    @PostMapping("/status/{status}")
    public Result updateStstus(@PathVariable Integer status,EmployeeDTO dto){
        log.info("【修改员工状态】");
        employeeService.updateEmployee(status,dto);
        return Result.success();
    }

    @ApiOperation("修改密码")
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("【修改员工密码】");
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }
}
